let util = require('../../utils/util');

let requestDomain = util.config('requestDomain');

let that;

// pages/personal/personal.js
Page({
  data:{
    profileList: [
      {
        profileId: 'userRealName',
        profileName: '真实名字',
        profileType: 'text',
        profileMaxLength: 8
      },
      {
        profileId: 'userPhoneNumber',
        profileName: '手机号码',
        profileType: 'number',
        profileMaxLength: 16
      }
    ]
  },

  onLoad:function() {

    that = this;
    
    that.getProfile();

  },

  onShow: function() {
      if (util.globalData('lastEditProfile')) {
          let {profileList} = that.data;
          let {profileId, profileVal} = util.globalData('lastEditProfile');
          for (let profileIndex in profileList) {
              if (profileList[profileIndex].profileId == profileId) {
                  profileList[profileIndex].profileVal = profileVal;
                  break;
              }
          }
          that.setData({ profileList: profileList });
      }
  },

  getProfile: function() {
    wx.showNavigationBarLoading();
    wx.showToast({
        title: '获取信息中',
        icon: 'loading',
        duration: 10000,
        mask: true
    });
    wx.request({
        url: requestDomain + 'user/getProfile',
        method: 'POST',
        data: {
            storeId: util.globalData('storeId'),
            _3rdSession: util.globalData('_3rdSession')
        },
        success: function(result) {
            wx.hideNavigationBarLoading();
            wx.hideToast();
            if(util.checkSuccess(result) && result.data && result.data.data) {
                let {profileList} = that.data;
                for (let i in profileList) {
                    profileList[i].profileVal = '- -';
                    for (let profileIndex in result.data.data) {
                        if (profileList[i].profileId == profileIndex) {
                            profileList[i].profileVal = (result.data.data[profileIndex] == '' ? '无' : result.data.data[profileIndex]);
                            break;
                        }
                    }
                }
                that.setData({
                    profileList: profileList
                });
            }
            else {
                util.showError('获取信息失败');
            }
        },
        fail: function() {
            wx.hideNavigationBarLoading();
            wx.hideToast();
            util.showError('获取信息失败', '发起获取个人简介信息请求失败');
        }
    });
  },

  editProfile: function(e) {
    let profileId = e.currentTarget.dataset.profileid;
    let {profileList} = that.data;
    for (let profileIndex in profileList) {
        if (profileList[profileIndex].profileId == profileId) {
            let {profileName, profileVal, profileType, profileMaxLength} = profileList[profileIndex];
            wx.navigateTo({
                url: '/pages/editProfile/editProfile?profileId=' + profileId + '&profileName=' + profileName + '&profileVal=' + profileVal + '&profileType=' + profileType + '&profileMaxLength=' + profileMaxLength,
                fail: function() {
                    wx.showModal({
                        title: '跳转失败',
                        content: '跳转到修改个人资料界面失败，请重试',
                    })
                }
            });
            break;
        }
    }
  }
})